C     Program EX_0501.FOR
C     Listing 9F - see documentation in TUTOR.SSS
 
$include:'SSSF1.H'
 
      subroutine prime
$include:'SSSF2.H'
      logical opens, repars
      integer ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
      real*8 inter, rept
      common opens, repars, inter, rept,
     +ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
 
      ARRIVL = 1
      STARTA = 2
      ENDACT = 3
      NEXTAC = 4
      STRTDY = 5
      CLOSES = 0
      WATCH  = 1

      call INIQUE(3, 1, 1)

      call INISTA(1,'Sojourn time         ',0,10,0.0,0.2)
      call CREATE(0.0, WATCH )
      call CREATE(0.5, CLOSES)
      call SIMEND(10.0)

      opens  = .TRUE.
      repars = .FALSE.
      inter  = 7.0/25.0
      rept   = 2.0/24.0
      return
      end

      subroutine clshop
$include:'SSSF2.H'
      logical opens, repars
      integer ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
      real*8 inter, rept
      common opens, repars, inter, rept,
     +ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
 
      opens = .FALSE.
  99  if (NQ(3).gt.0) then
        call REMVFQ(3, 1)
        call TALLY(1, T() - A(1))
        call DISPOS
        goto 99
      endif
      return
      end
 
      subroutine newday
$include:'SSSF2.H'
      logical opens, repars
      integer ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
      real*8 inter, rept
      common opens, repars, inter, rept,
     +ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
 
      call CREATE(0.5, CLOSES)
      call DISPOS
      opens  = .TRUE.
      repars = .FALSE.
  99  if (NQ(1).gt.0) then
        call REMVFQ(1, 1)
        call QUEUE(2, 0.0)
        goto 99
      endif
      return
      end
 
      Program EX_0501
$include:'SSSF2.H'
      logical opens, repars
      integer ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
      real*8 inter, rept
      common opens, repars, inter, rept,
     +ARRIVL, STARTA, ENDACT, NEXTAC, STRTDY,
     +CLOSES, WATCH
      integer ecode
 
      call prime
   99 ecode = NEXTEV()
      if (ecode.gt.0) then
        goto (101, 102, 103, 104, 105) ecode
 
C ARRIVL
  101   continue
        if (IDE().eq.WATCH) then
          call CREATE(EX(inter), WATCH)
          call SETA(1, T())
          call SCHED(0.0, NEXTAC, WATCH)
        else
          call SCHED(0.5, STRTDY, CLOSES)
          call clshop
        endif
        goto 99
 
C NEXTAC
  104   continue
        if (opens) then
          if (repars) then
            call QUEUE(2, 0.0)
          else
            call SCHED(0.0, STARTA, IDE())
          endif
        else
          call QUEUE(1, 0.0)
        endif
        goto 99
 
C STARTA
  102   continue
        call SCHED(EX(rept), ENDACT, 0)
        repars = .TRUE.
        goto 99
 
C ENDACT
  103   continue
        call QUEUE(3, 0.0)
        if (NQ(2).gt.0) then
          call REMVFQ(2, 1)
          call SCHED(0.0, STARTA, 0)
        else
          repars = .FALSE.
        endif
        goto 99
 
C STRTDY
  105   continue
        call newday
        goto 99
 
      else
 
        call SUMRY(' ')
        stop 'End of simulation'
 
      endif
      end
